Imports KellermanSoftware.SharpZipWrapper

Public Class frmMain

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("Are you sure you want to exit?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton2, "Exit confirmation") <> MsgBoxResult.Yes Then
            e.Cancel = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dlgSave.ShowDialog()
        txtFileName.Text = dlgSave.FileName
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim var As System.Collections.DictionaryEntry
        Dim i As Integer

        For Each var In System.Environment.GetEnvironmentVariables
            i = environGrid.Rows.Add()
            environGrid.Rows(i).Cells(0).Value = "%" & var.Key & "%"
            environGrid.Rows(i).Cells(1).Value = var.Value
        Next

        dlgAUexe.InitialDirectory = Application.StartupPath
        dlgOpen.InitialDirectory = Application.StartupPath
        dlgSave.InitialDirectory = Application.StartupPath
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim helpStr As String

        helpStr = "This generator makes it easier to create an autoupdate for ERN."
        helpStr = helpStr & ControlChars.CrLf & "1) Select the filename for the autoupdate - it will be a ZIP file."
        helpStr = helpStr & ControlChars.CrLf & "2) Select the location of the autoupdate engine file.  This is the mini-program that actually performs the update that you are building now."
        helpStr = helpStr & ControlChars.CrLf & "3) Select the new version for the update.  A.B.C.D.E where A is a major release, B is a Feature-Set release (bunch of features), C is a Feature release (one feature), D is an incremental release (a piece of a feature), and E is a critical patch (security or the like)."
        helpStr = helpStr & ControlChars.CrLf & "4) An update consists of files that are copied to the client's system.  This can be done in two ways..."
        helpStr = helpStr & ControlChars.CrLf & "4a) Select files to include in the update by clicking the 'Add File...' button (you may select more than one file)."
        helpStr = helpStr & ControlChars.CrLf & "4b) If you want to include all of the files in a folder use the 'Add folder...' button."
        helpStr = helpStr & ControlChars.CrLf & "5) File are listed in the file list and you can delete them by right clicking.  For each file, you can specify if the file's version should be changed to match the new version, if when the update is applied and the file exists if it should be overwritten or not, and also the final location of the file on the client system (this will typically be %ERN% but can be anything - if a path is unknown when the update is applied the default ERN location will be used) - do not include the final backslash."
        helpStr = helpStr & ControlChars.CrLf & "6) The Environment Variables list shows you what variables your current system knows and can be used above when specifing the destincation location in the file list."
        helpStr = helpStr & ControlChars.CrLf & "7) The Batch Script text box allows you to run any command you want after the files have been copied to the client's machine.  A batch file is created by using the text you enter.  No error checking is done or any safeguards."
        helpStr = helpStr & ControlChars.CrLf & "8) The 'Create AU' button actually takes all of the information you've supplied and attempts to create an autoupdate file for you to then upload to the ERN server and deploy to your clients.  If any errors occur then this process will be terminated."
        helpStr = helpStr & ControlChars.CrLf & "9) The Help button give you this exteremly helpful and entertaining message box!"
        helpStr = helpStr & ControlChars.CrLf & "10) Once you are done or fed up with creating an autoupdate, click 'Exit'!"
        MsgBox(helpStr)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim fn As String
        Dim i As Integer

        If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each fn In dlgOpen.FileNames
                i = gridFiles.Rows.Add
                gridFiles.Rows(i).Cells(0).Value = True
                gridFiles.Rows(i).Cells(1).Value = fn
                gridFiles.Rows(i).Cells(2).Value = IO.Path.GetFileName(fn)
                gridFiles.Rows(i).Cells(3).Value = True
                gridFiles.Rows(i).Cells(4).Value = "%ERN%"
            Next
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim i As Integer

        If dlgFolder.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim dir As New IO.DirectoryInfo(dlgFolder.SelectedPath)
            Dim files As IO.FileInfo() = dir.GetFiles
            Dim file As IO.FileInfo

            If MsgBox("Are you sure you want to add all " & files.Length & " file(s)?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton1, "Multiple file add confirmation") = MsgBoxResult.Yes Then
                For Each file In files
                    i = gridFiles.Rows.Add
                    gridFiles.Rows(i).Cells(0).Value = True
                    gridFiles.Rows(i).Cells(1).Value = file.FullName
                    gridFiles.Rows(i).Cells(2).Value = file.Name
                    gridFiles.Rows(i).Cells(3).Value = True
                    gridFiles.Rows(i).Cells(4).Value = "%ERN%"
                Next
            End If
        End If
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        If MsgBox("Are you sure you want to delete '" & gridFiles.Rows(gridFiles.SelectedCells(0).RowIndex).Cells(2).Value & "' from the file list?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton1, "Delete confirmation") = MsgBoxResult.Yes Then
            gridFiles.Rows.Remove(gridFiles.Rows(gridFiles.SelectedCells(0).RowIndex))
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        dlgAUexe.ShowDialog()
        txtAUexe.Text = dlgAUexe.FileName
    End Sub

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Dim au As New ZipHelper
        Dim auDetails As IO.StreamWriter
        Dim auBatch As IO.StreamWriter
        Dim i As Integer
        Dim everythingOK As Boolean = True

        If txtFileName.Text = "" Then
            MsgBox("You need to provide the filename for the generated autoupdate.")
            txtFileName.Focus()
            everythingOK = False
        Else
            If IO.File.Exists(txtFileName.Text) Then
                If MsgBox("The destination update already exists (" & txtFileName.Text & ").  Do you want to overwrite it?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
                    IO.File.Delete(txtFileName.Text)
                Else
                    everythingOK = False
                End If
            End If
        End If

        If txtAUexe.Text = "" Then
            If IO.File.Exists(IO.Path.GetDirectoryName(Application.ExecutablePath) & "\au.exe") Then
                txtAUexe.Text = IO.Path.GetDirectoryName(Application.ExecutablePath) & "\au.exe"
                MsgBox("You left the autoupdate engine filename blank.  So the one located in the current directory will be used.")
            Else
                MsgBox("You have not provided the location of the autoupdate engine.")
                everythingOK = False
                txtAUexe.Focus()
            End If
        End If

        If gridFiles.Rows.Count = 0 Then
            MsgBox("No update can be created since you have not selected any files to include in the update.")
            everythingOK = False
        End If

        If n1.Value = 0 And n2.Value = 0 Then
            If n3.Value = 0 And n4.Value = 0 Then
                If everythingOK Then
                    If Not MsgBox("You have not changed the version numbers.  Are you sure you want the update to be marked as version 0.0.0.0.0?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
                        everythingOK = False
                    End If
                End If
            End If
        End If

        If everythingOK Then
            'create batch file if script exists
            If txtAUBatch.Text <> "" Then
                auBatch = IO.File.CreateText("auBatch.bat")
                auBatch.WriteLine(txtAUBatch.Text)
                auBatch.Close()
            End If

            'create text file describing what files go where and overwrite
            auDetails = IO.File.CreateText("auDetails.txt")

            auDetails.WriteLine("File Version:" & n1.Value & "." & n2.Value & "." & n3.Value & "." & n4.Value)
            auDetails.WriteLine("File Count:" & gridFiles.Rows.Count)

            For i = 0 To gridFiles.Rows.Count - 1
                auDetails.WriteLine(gridFiles.Rows(i).Cells(0).Value & "::" & gridFiles.Rows(i).Cells(3).Value & "::" & gridFiles.Rows(i).Cells(2).Value & "::" & gridFiles.Rows(i).Cells(4).Value)
                everythingOK = au.AddFilesToZip(txtFileName.Text, IO.Path.GetDirectoryName(gridFiles.Rows(i).Cells(1).Value), gridFiles.Rows(i).Cells(2).Value, False, "")
            Next

            If txtAUBatch.Text <> "" Then
                auDetails.WriteLine("Run Batch:True")
            Else
                auDetails.WriteLine("Run Batch:False")
            End If

            auDetails.Close()

            'zip everything up
            If txtAUBatch.Text <> "" Then au.AddFilesToZip(txtFileName.Text, ".", "auBatch.bat", False, "")
            everythingOK = au.AddFilesToZip(txtFileName.Text, ".", "auDetails.txt", False, "")
            everythingOK = au.AddFilesToZip(txtFileName.Text, IO.Path.GetDirectoryName(txtAUexe.Text), IO.Path.GetFileName(txtAUexe.Text), False, "")
            'if the dsofile.dll exists with the auengine file then include it!
            IO.File.Delete("auDetails.txt")
            If txtAUBatch.Text <> "" Then IO.File.Delete("auBatch.bat")
            If Not everythingOK Then
                MsgBox("Error creating autoupdate.  Possibly because a required file could not be added.")
                IO.File.Delete(txtFileName.Text)
            Else
                MsgBox("Done!  Autoupdate created successfully!")
            End If
        End If
    End Sub
End Class
